<template>
<div class="col-md-12">
  <rd-widget>
    <rd-widget-body>
      <form method="post">
        <div class="form-group">
          <span>{{ $t("form.maxTrySignInFailTimes") }}</span>
          <div class="input-group"> 
            <input class="form-control" type="range" min="1" max="5" step="1" v-model="form.maxTrySignInFailTimes" />
            <span class="input-group-addon">{{form.maxTrySignInFailTimes}}</span>
          </div>
        </div>
        <hr />
        <div class="checkbox">
          <label>
            <input type="checkbox" v-model="form.forceUpdatePassword.enable" :true-value=1 :false-value=0> {{ $t("form.forceUpdatePassword.enable") }}
          </label>
        </div>
        <div class="form-group">
          <span>{{ $t("form.forceUpdatePassword.daysOfPeriod") }}</span>
          <select class="form-control" v-model="form.forceUpdatePassword.daysOfPeriod">
            <option v-for="option in timeOptions" :value="option.value">{{option.text}}</option>
          </select>
        </div>
        <span>{{ $t("form.forceUpdatePassword.applyToRoles") }}</span>
        <div class="checkbox">
          <label>
            <input type="checkbox" v-model="applyToRoles.webSysAdmin" :true-value=1 :false-value=0> {{ $t("role.webSysAdmin") }}
          </label>
          <label>
            <input type="checkbox" v-model="applyToRoles.webSecAdmin" :true-value=1 :false-value=0> {{ $t("role.webSecAdmin") }}
          </label>
          <label>
            <input type="checkbox" v-model="applyToRoles.webAuditor" :true-value=1 :false-value=0> {{ $t("role.webAuditor") }}
          </label>
          <label>
            <input type="checkbox" v-model="applyToRoles.webUser" :true-value=1 :false-value=0> {{ $t("role.webUser") }}
          </label>
        </div>
        <button type="button" class="btn btn-default" @click.prevent="onSubmit">{{ $t("common.save") }}</button>
      </form>
    </rd-widget-body>
  <rd-widget>
</div>
</template>

<script>
import _ from 'lodash'
import toast from 'libs/toast.js'
import msgbox from 'libs/msgbox.js'
import {http} from 'share/server/api.js'
import locales from '../locales/authSecPolicy.js'

export default {
  locales,
  data () {
    return {
      form: {
        maxTrySignInFailTimes: 3,
        forceUpdatePassword: {
          enable: 0,
          daysOfPeriod: 7,
          applyToRoles: '[]'
        }
      },
      applyToRoles: {
        webSysAdmin: 0,
        webSecAdmin: 0,
        webAuditor: 0,
        webUser: 0       
      },
      timeOptions: [{
        value: 7,
        text: this.$t("time.week")
      }, {
        value: 14,
        text: this.$t("time.weeks", {num: 2})
      }, {
        value: 30,
        text: this.$t("time.month")
      }, {
        value: 90,
        text: this.$t("time.quarter")
      }, {
        value: 365,
        text: this.$t("time.year")
      }]
    }
  },
  vuex: {
  },
  methods: {
    onSubmit () {
      var applyToRoles = []
      _.forEach(this.applyToRoles, function(value, key) {
        if (value === 1) {
          applyToRoles.push(key)
        }
      })
      this.form.forceUpdatePassword.applyToRoles = JSON.stringify(applyToRoles)
      http('/api/auth/settings', 'PUT', this.form).then(function(res) {
        if (res && res.data && res.data.code === 0) {
          toast.s(this, this.$t('common.success'), '')
        } else {
          toast.code(this, res.data)
        }
      }.bind(this))      
    },
    loadData (data) {
      this.form = data

      let applyToRoles = JSON.parse(this.form.forceUpdatePassword.applyToRoles)
      _.forEach(applyToRoles, function(item) {
        if (typeof this.applyToRoles[item] != 'undefined') {
          this.applyToRoles[item] = 1
        }        
      }.bind(this))
    }
  },
  ready () {
    http('/api/auth/settings', 'GET').then(function(res) {
      if (res && res.data && res.data.code === 0) {
        this.loadData(res.data.data)
      } else {
        toast.code(this, res.data)
      }
    }.bind(this))
  }
}
</script>

<style>
</style>